//用户的信息

import { defineStore } from 'pinia';
//引入接口
import { reqlogin } from '../api/user/index'
//引入路由
import { constroute } from '@/router/routes';
//引入数据类型
import bus from "@/utils/bus";


import type { loginForim, loginres } from '../api/user/type'
import type { userstatus } from './types'
import { set_token, get_token } from '@/utils/token'
let userstore = defineStore('user', {
    state: (): userstatus => {
        //存数据
        return {
            token: get_token(),
            menu: constroute//仓库生成菜单需要的数组（路由）
        }
    },
    //异步或者逻辑 
    actions: {
        //用户登录的方法
        async userlogin(data: loginForim) {

            let res: loginres = await reqlogin(data)

            
            if (res.code == 200) {
                
                this.token = (res.data.tokenValue as string)
                // localStorage.setItem('token',(res.data.token as string))
                set_token(res.data.tokenValue as string)
                bus.emit("isshow", true);
                // window.open('http://portal.whshanghui.com?' + 'token=' + res.data.tokenValue )
                                
                return 'ok'
            } else {
                return Promise.reject(res)
            }

        }
    },
    getters: {

    }
})

export default userstore
